<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class SetAdminPassword extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'admin:pw';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '重新设置系统管理员密码';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $str_Password = $this->secret('请输入新密码:');
        $str_Repassword = $this->secret('请再次输入新密码:');
        if($str_Password != $str_Repassword){
            $this->error('两次输入的密码不一致!');
            return;
        }
        if ($this->confirm('确定要修改密码吗?')) {
            $path = base_path('.env');

            if (file_exists($path)) {
                file_put_contents($path, str_replace(
                    'ADMIN_USER_PASSWD='.env('ADMIN_USER_PASSWD'), 'ADMIN_USER_PASSWD='.md5($str_Password), file_get_contents($path)
                ));
            }
        }
    }
}
